<?php
	class Kiosk extends MY_Controller{
		
		public function __construct(){
			parent::__construct();
			$this->load->model('kiosk_model');
			$this->load->model('location_model');
		}
		
		public function index($limit=FALSE){	
			$this->load->helper("form");
			
			//set the limit or offset for each page
			$this->kiosk_model->pager($limit);
			
			// define the link for pagination
			$my_link = site_url('kiosk');
			
			$data['kiosk'] = $this->kiosk_model->get_kiosk();
			
			// call the pagination from parent controller			
			$this->pager($my_link, $this->kiosk_model->get_total(), $this->kiosk_model->get_record_per_page());
			
			$data['title'] = "Kiosk list";
			$this->load->view('templates/header1', $data);
			$this->load->view('kiosk/index', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');			
		}
		
		public function search($src=FALSE,$limit=FALSE){
			$this->load->helper("form");
			$data['title'] = "Kiosk list";
			$search_text = '';
			
			//set the limit or offset for each page
			$this->kiosk_model->pager($limit);			
			
			if($src!=FALSE){				
				$data['kiosk'] = $this->kiosk_model->search($src);		
				$search_text = $src;
			}
			else{
				$search_text = $this->input->post('search_text');				
				$data['kiosk'] = $this->kiosk_model->search($search_text);		
			}
			
			// define the link for pagination
			$my_link = site_url('kiosk/search/'.$search_text);
			
			// call the pagination from parent controller
			$this->pager($my_link, $this->kiosk_model->get_total_search($search_text), $this->kiosk_model->get_record_per_page(), 4);
			
			$this->load->view('templates/header1', $data);
			$this->load->view('kiosk/index', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');
		}
		
		
		public function displayForm($id=FALSE){			
			$this->load->helper("form");
			$data = array();
			if($id!=FALSE){
				$data['kiosk'] = $this->kiosk_model->get_kiosk($id);							
			}
			$data['title']="Create Kiosk";
			$data['location'] = $this->location_model->get_location_list();
			$this->load->view('templates/header1', $data);
			$this->load->view('kiosk/form', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');			
		}
		
		public function create(){
			$this->displayForm();
		}
		
		public function edit($id){
			$this->displayForm($id);
		}
		
		public function delete($id){
			$this->kiosk_model->delete($id);
			redirect("kiosk", "refresh");
		}
		
		public function save(){			
			$this->load->helper("form");
			$this->load->library('form_validation');
			$id = $this->input->post('id');
			
			$this->form_validation->set_rules('name', 'Name', 'callback_name_check');
			$this->form_validation->set_rules('mac_address', 'MacAddress', 'callback_mac_check');
			if(empty($id)){
				$this->form_validation->set_rules('password', 'Password', 'required');
			}
			if ($this->form_validation->run() === FALSE){
				$data['title']="Kiosk";
				$data['location'] = $this->location_model->get_location_list();
				$this->load->view('templates/header1', $data);
				$this->load->view('kiosk/form', $data);
				$this->load->view('templates/right', $data);
				$this->load->view('templates/footer');
			}
			else{
				$this->kiosk_model->save();
				redirect("kiosk", "refresh");
			}			
		}
		
		public function mac_check(){
			$mac_address = trim($this->input->post('mac_address'));
			$id = trim($this->input->post('id'));
			
			if(empty($id)) $id=FALSE;
			
			if(empty($mac_address)){
				$this->form_validation->set_message('mac_check', 'The MacAddress is required');
				return FALSE;
			}
			if($this->kiosk_model->check_mac_address($id, $mac_address)===FALSE){
				$this->form_validation->set_message('mac_check', 'The MacAddress is already exists');
				return FALSE;
			}
			return TRUE;
		}
		
		public function name_check(){
			$name = trim($this->input->post('name'));
			$id = trim($this->input->post('id'));
			
			if(empty($id)) $id=FALSE;
			
			if(empty($name)){
				$this->form_validation->set_message('name_check', 'The kiosk name is required');
				return FALSE;
			}
			
			if($this->kiosk_model->check_kiosk_name($id, $name)===FALSE){
				$this->form_validation->set_message('name_check', 'The name is already exists');
				return FALSE;
			}
			return TRUE;
		}
		
	}
?>